package com.zhm.demo.print;

import cn.hutool.core.util.ReflectUtil;
import dnl.utils.text.table.TextTable;

import java.lang.reflect.Field;
import java.util.List;

public class TablePrint {

    public static <T> void print(List<T> list, Class<T> clazz) {
        Field[] fields = ReflectUtil.getFields(clazz);
        String[] titles = new String[fields.length];
        for (int i = 0; i < fields.length; i++) {
            titles[i] = fields[i].getName();
        }
        Object[][] datas = new Object[list.size()][titles.length];
        for (int j = 0; j < list.size(); j++) {
            Object[] data = new Object[fields.length];
            for (int i = 0; i < fields.length; i++) {
                data[i] = ReflectUtil.getFieldValue(list.get(j), fields[i]);
            }
            datas[j] = data;
        }
        TextTable tt = new TextTable(titles, datas);
        tt.printTable();
    }

    public static void main(String[] args) {
//        List<IotModelParam> iotModelParams = new ArrayList<>();
//        for (int i = 0; i < 10; i++) {
//            IotModelParam iotModelParam = new IotModelParam();
//            iotModelParam.setId(i + 1L);
//            iotModelParam.setName("name" + i);
//            iotModelParam.setModelId(i + 1L);
//            iotModelParams.add(iotModelParam);
//        }
//        print(iotModelParams, IotModelParam.class);

    }
}
